package com.haoji.upload.rocketmq;

import com.alibaba.fastjson.JSON;
import com.haoji.upload.dto.MqMessageDTO;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: upload
 * @description:
 * @author: xiaolei
 * @create: 2021-04-21 09:46
 **/
@Service
public class MqProducerServiceImpl implements MqProducerService {

    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @Override
    public boolean sendMessage(MqMessageDTO mqMessageDTO) {
        SendResult sendResult;
        try {
            Message message = new Message(mqMessageDTO.getTopic(), mqMessageDTO.getTag(), mqMessageDTO.getKey(), mqMessageDTO.getContent());
            sendResult = defaultMQProducer.send(message);
        } catch (Exception e) {
            return false;
        }
        if (SendStatus.SEND_OK.equals(sendResult.getSendStatus())) {
            mqMessageDTO.setMsgId(sendResult.getMsgId());
            return true;
        }
        return false;
    }
}
